package com.blog.blogservice.util;


import org.commonmark.Extension;
import org.commonmark.ext.gfm.tables.TablesExtension;
import org.commonmark.ext.heading.anchor.HeadingAnchorExtension;
import org.commonmark.node.Image;
import org.commonmark.node.Node;
import org.commonmark.parser.Parser;
import org.commonmark.renderer.html.AttributeProvider;
import org.commonmark.renderer.html.AttributeProviderContext;
import org.commonmark.renderer.html.AttributeProviderFactory;
import org.commonmark.renderer.html.HtmlRenderer;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author guanglin.ma
 * @date 2024-11-13 9:51
 */
public class MarkdownToHtmlUtils {

    /**
     * 增加扩展[标题锚点，表格生成]
     * Markdown转换成HTML
     *
     * @param markdown
     * @return
     */
    public static String markdownToHtmlExtensions(String markdown) {
        //h标题生成id
        Set<Extension> headingAnchorExtensions = Collections.singleton(HeadingAnchorExtension.create());
        //转换table的HTML
        List<Extension> tableExtension = Collections.singletonList(TablesExtension.create());
        Parser parser = Parser.builder()
                .extensions(tableExtension)
                .build();
        Node document = parser.parse(markdown);
        HtmlRenderer renderer = HtmlRenderer.builder()
                .extensions(headingAnchorExtensions)
                .extensions(tableExtension)
                .attributeProviderFactory(new AttributeProviderFactory() {
                    public AttributeProvider create(AttributeProviderContext context) {
                        return new CustomAttributeProvider();
                    }
                })
                .build();
        return renderer.render(document);
    }

    /**
     * 处理标签的属性
     */
    static class CustomAttributeProvider implements AttributeProvider {
        @Override
        public void setAttributes(Node node, String tagName, Map<String, String> attributes) {
//            //改变a标签的target属性为_blank
//            if (node instanceof Link) {
//                attributes.put("target", "_blank");
//            }
//            if (node instanceof TableBlock) {
//                attributes.put("class", "ui celled table");
//            }
            //解决图片标签访问问题
            if (node instanceof Image){
                //referrerpolicy="no-referrer"
                attributes.put("referrerpolicy", "no-referrer");
            }
        }
    }

    /**
     * 完整Html
     *
     * @param markdown
     * @return
     */
    public static String splitHtml(String markdown, String title) {
        String markHtml = markdownToHtmlExtensions(markdown);
        return htmlTemplate(markHtml, title);
    }

    /**
     * 拼接 模板
     *
     * @return
     */
    private static String htmlTemplate(String mdHtmlStr, String title) {
        String html = "<!DOCTYPE html>\n" +
                "<html lang=\"zh-CN\">\n" +
                "\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n" +
                "    <title>" + title + "+</title>\n" +
                "    <link rel=\"stylesheet\"\n" +
                "        href=\"https://cdnjs.cloudflare.com/ajax/libs/github-markdown-css/5.1.0/github-markdown.min.css\" />\n" +
                "    <link rel=\"stylesheet\" href=\"https://cdnjs.cloudflare.com/ajax/libs/prism/1.26.0/themes/prism.min.css\" />\n" +
                "    <link rel=\"stylesheet\"\n" +
                "        href=\"https://cdnjs.cloudflare.com/ajax/libs/prism/1.26.0/plugins/line-numbers/prism-line-numbers.min.css\" />\n" +
                "    <style>\n" +
                "       \n" +
                "        body {\n" +
                "            color: #333;\n" +
                "            font-family: 'Arial', sans-serif;\n" +
                "            margin: 0;\n" +
                "            padding: 20px;\n" +
                "            background-color: #ddd;\n" +
                "        }\n" +
                "\n" +
                "        .markdown-body {\n" +
                "            box-sizing: border-box;\n" +
                "            min-width: 200px;\n" +
                "            max-width: 1000px;\n" +
                "            margin: 0 auto;\n" +
                "            padding: 45px;\n" +
                "            background-color: #fff;\n" +
                "            border-radius: 8px;\n" +
                "            transition: all 0.3s ease-in-out;\n" +
                "        }\n" +
                "\n" +
                "        @media (max-width: 767px) {\n" +
                "            .markdown-body {\n" +
                "                padding: 15px;\n" +
                "            }\n" +
                "        }\n" +
                "\n" +
                "        h1,\n" +
                "        h2,\n" +
                "        h3,\n" +
                "        h4,\n" +
                "        h5,\n" +
                "        h6 {\n" +
                "            color: #2c3e50;\n" +
                "            font-weight: 700;\n" +
                "        }\n" +
                "\n" +
                "        p {\n" +
                "            line-height: 1.6;\n" +
                "            color: #333;\n" +
                "            margin-bottom: 1em;\n" +
                "        }\n" +
                "\n" +
                "        a {\n" +
                "            color: #3498db;\n" +
                "            text-decoration: none;\n" +
                "        }\n" +
                "\n" +
                "        a:hover {\n" +
                "            text-decoration: underline;\n" +
                "        }\n" +
                "\n" +
                "        th,\n" +
                "        td {\n" +
                "            padding: 12px 15px;\n" +
                "            text-align: left;\n" +
                "            border: 1px solid #ddd;\n" +
                "            background-color: #f8f9fa;\n" +
                "            color: #100404;\n" +
                "            font-weight: 700;\n" +
                "        }\n" +
                "\n" +
                "        li {\n" +
                "            color: #333;\n" +
                "        }\n" +
                "        \n" +
                "        /* 公式显示问题待解决 */\n" +
                "    </style>\n" +
                "</head>\n" +
                "\n" +
                "<body>\n" +
                "    <div class=\"markdown-body\">\n" +
                "        <article>\n" +
                "            <div>\n" +
                "               <h2>" + title + "</h2>\n" +
                "                \n" + mdHtmlStr +
                "            </div>\n" +
                "        </article>\n" +
                "    </div>\n" +
                "    <script src=\"https://cdnjs.cloudflare.com/ajax/libs/prism/1.26.0/components/prism-core.min.js\"></script>\n" +
                "    <script\n" +
                "        src=\"https://cdnjs.cloudflare.com/ajax/libs/prism/1.26.0/plugins/autoloader/prism-autoloader.min.js\"></script>\n" +
                "    <script\n" +
                "        src=\"https://cdnjs.cloudflare.com/ajax/libs/prism/1.26.0/plugins/line-numbers/prism-line-numbers.min.js\"></script>\n" +
                "</body>\n" +
                "\n" +
                "</html>";
        return html;
    }


}
